package com.chen.tcp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

// 服务端
public class ServerSocketTest {
    public static void main(String[] args) throws IOException {

        // 1、创建端口号
        ServerSocket serverSocket = new ServerSocket(9999);
        // 2、等待客户端连接
        Socket socket = serverSocket.accept();
        // 3、获取客户端信息
        InputStream in = socket.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        System.out.println(baos.toString());

        // 关闭资源
        baos.close();
        in.close();
        socket.close();
        serverSocket.close();

    }
}
